/*****************************************************************************
 *   Copyright 2011 ZHUANG Yuan              	                             *
 *   SudokuITS as Final Year Project for BScCS, City University of Hong Kong *
 *                                      									 *
 *   Licensed under the Apache License, Version 2.0 (the "License");         *
 *   you may not use this file except in compliance with the License.        *
 *   You may obtain a copy of the License at                                 *
 *                                                                           *
 *   http://www.apache.org/licenses/LICENSE-2.0                              *
 *                                                                           *
 *   Unless required by applicable law or agreed to in writing, software     *
 *   distributed under the License is distributed on an "AS IS" BASIS,       *
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*
 *   See the License for the specific language governing permissions and     *
 *   limitations under the License.                                          *
 *****************************************************************************/

package android.sudoku.its.views;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.sudoku.its.views.R;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Sudoku Main Menu
 * @author 51158282
 *
 */

public class MainMenuPage extends Activity implements OnClickListener {
	public static final String USER_TYPE = "user type";
	public static final String NEW_USER = "new user";
	public static final String USER_TITLE = "title";
	public static final String DEFAULT_USER_TITLE = "Guest";
    private String userTitle = "";    
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set layout for Main Menu Page
        setContentView(R.layout.main_menu);
        
        // Set greeting message according to the user type
        userTitle = DEFAULT_USER_TITLE;
        if (getIntent().getStringExtra(USER_TITLE) != null)
        	userTitle = getIntent().getStringExtra(USER_TITLE);
        TextView greetingView = (TextView) findViewById(R.id.greeting);
        greetingView.append(userTitle);
        
        // Set onClickListner for buttons		
		ImageView resumeButton = (ImageView) findViewById(R.id.resume_button);
        resumeButton.setOnClickListener(this);
        ImageView newGameButton = (ImageView) findViewById(R.id.new_game_button);
        newGameButton.setOnClickListener(this);
        ImageView statButton = (ImageView) findViewById(R.id.stat_button);
        statButton.setOnClickListener(this);
        ImageView settingsButton = (ImageView) findViewById(R.id.settings_button);
        settingsButton.setOnClickListener(this);
        ImageView sudokuBasicsButton = (ImageView) findViewById(R.id.sudoku_basics_button);
        sudokuBasicsButton.setOnClickListener(this);
        ImageView aboutButton = (ImageView) findViewById(R.id.about_button);
        aboutButton.setOnClickListener(this);
        
        // If this is a new user, pop up a dialogue to alert the user to learn the terminologies that will be used in instructions
        // If this page is redirected from "Try it first" in Entry Page or from Register Page, this user is treated as new user
        if ((getIntent().getStringExtra(USER_TYPE) != null) && (getIntent().getStringExtra(USER_TYPE).equals(NEW_USER))) 
        	diplayTermLearningAlert();   
    }

	public void onClick(View v) {
		switch(v.getId()) {
			case R.id.resume_button:
				startGame(GamePage.NULL_DIFFICULT_LEVEL_INDICATOR, GamePage.RESUME_INDICATOR);	
				break;
			case R.id.new_game_button:
				openDifficultyLevelDialog();
				break;
			case R.id.settings_button:
				startSettingsPage();
				break;
			case R.id.sudoku_basics_button:
				startSudokuBasicsPage();
				break;
			case R.id.about_button:
				startAboutPage();
				break;		
		}
	}
	
	/**
	 * Alert the user to learn the terminologies that will be used in instructions
	 */
	private void diplayTermLearningAlert() {		
		String alertMsg = getResources().getString(R.string.term_learning_alert_msg);
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();		
		alertDialog.setTitle(getResources().getString(R.string.new_user_greeting));
		alertDialog.setMessage(alertMsg);
		alertDialog.setButton("Learn terms", new DialogInterface.OnClickListener() {			
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				startSudokuBasicsPage();			
			}
		});
		alertDialog.setButton2("I've already learned", new DialogInterface.OnClickListener() {			
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();				
			}
		});		
		alertDialog.show();		
	}
	
	/**
	 * Display the Difficulty Level Dialog
	 */
	private void openDifficultyLevelDialog() {
		new AlertDialog.Builder(this)
			.setTitle(R.string.difficulty_level_title)
			.setItems(R.array.difficulty_level, 
				new DialogInterface.OnClickListener() {					
					public void onClick(DialogInterface dialog, int which) {
						startGame(which, GamePage.NOT_RESUME_INDICATOR);
					}
				}).show();
	}
	
	/**
	 * Start a new game of the chosen difficulty level
	 * @param diff		difficulty level
	 */
	private void startGame(int diff, int ifResume) {
		Intent i  = new Intent(this, GamePage.class);
		i.putExtra(GamePage.DIFF_LEVEL, diff);
		i.putExtra(GamePage.RESUME_THE_GAME, ifResume);
		i.putExtra(USER_TITLE, userTitle);
		startActivity(i);		
	}
	
	/**
	 * Go to Settings page
	 */
	private void startSettingsPage() {
		Intent i = new Intent(getBaseContext(), SettingsPage.class);
		startActivity(i);		
	}
	
	private void startSudokuBasicsPage() {
		Intent i = new Intent(this, SudokuBasics.class);
		startActivity(i);
	}
	
	/**
	 * Go to About page
	 */
	private void startAboutPage() {
		Intent i = new Intent(this, AboutPage.class);
		startActivity(i);		
	}
	
	
	
	
}